<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>业务场景列表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="shortcut icon" href="${ctx }/ico/favicon.ico">
<link href="${ctx }/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link href="${ctx }/css/font-awesome.css?v=4.4.0" rel="stylesheet">

<!-- jqgrid-->
<link href="${ctx }/css/plugins/jqgrid/ui.jqgrid.css?0820"
	rel="stylesheet">

<link href="${ctx }/css/animate.css" rel="stylesheet">
<link href="${ctx }/css/common.css" rel="stylesheet">
<link href="${ctx }/css/style.css?v=4.1.0" rel="stylesheet">
<!-- Sweet Alert -->
<link href="${ctx }/css/plugins/sweetalert/sweetalert.css"
	rel="stylesheet">
<link
	href="${ctx}/css/plugins/bootstrap-select/bootstrap-select.min.css"
	rel="stylesheet" />
<!-- bootstrap-switch css -->
<link href="${ctx }/css/plugins/bootstrap-switch/bootstrap-switch.css"
	rel="stylesheet">
</head>
<body class="gray-bg">
	<div class="animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox ">
					<div class="ibox-content">
						<div class="row m-b-sm m-t-sm">
							<div class="col-md-11">
								<div class="input-group">
									<button class="btn btn-success btn-outline" type="button"
										id="addScene">
										<i class="fa fa-plus"></i> 添加
									</button>
									<button class="btn btn-success btn-outline" type="button"
										id="editScene">
										<i class="fa fa-pencil"></i> 编辑
									</button>
									<button class="btn btn-success btn-outline" type="button"
										id="delScene">
										<i class="fa fa-trash"></i> 删除
									</button>
								</div>
							</div>
						</div>
						<div class="jqGrid_wrapper">
							<table id="table_list"></table>
							<div id="pager_list"></div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>


	<!-- 添加菜单模态窗口 -->
	<div class="modal inmodal" id="addSceneWin" tabindex="-1" role="dialog"
		aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content animated bounceInRight">
				<div class="modal-header">
					<button type="button" class="close closeWin" data-dismiss="modal">
						<span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
					</button>
					<h5 class="modal-title">场景配置</h5>
				</div>

				<div class="ibox-content">
					<form class="form-horizontal m-t" id="addSceneForm">
						<div class="form-group">
							<label class="col-sm-3 control-label">场景名称：</label>
							<div class="col-sm-8">
								<input id="scene_name" name="scene_name" class="form-control"
									required="" aria-required="true" type="text">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label">管理部门：</label>
							<div class="col-sm-8">
								<select class="selectpicker" required="" aria-required="true"
									name="org_id" id="org_id">
									<option value="">--请选择--</option>
									<c:forEach items="${olist}" var="orgInfo">
										<option value="${orgInfo.org_id}">${orgInfo.org_name}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-3 control-label">监控开关：</label>
							<div class="col-sm-2">
								<div class="switch has-switch">
									<div id="share_switch_div" class="switch-on switch-animate">
										<input id="share_switch_input" name="share_switch_input"
											type="checkbox" checked="checked">
									</div>
								</div>
								<input id="scene_status" name="scene_status" type="hidden">
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-8 col-sm-offset-3">
								<button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
								<button type="submit" class="btn btn-primary">保存</button>
							</div>
						</div>
					</form>
				</div>

			</div>
		</div>
	</div>
	<!--end 添加代码模态窗口 -->


	<!-- 修改代码模态窗口 -->
	<div class="modal inmodal" id="editSceneWin" role="dialog"
		aria-hidden="true">
		<iframe id="editSceneFrame" width="100%" height="100%"
			frameborder="0"></iframe>
	</div>
	<!--end 修改菜单模态窗口 -->

	<!-- 全局js -->
	<script src="${ctx }/js/jquery.min.js?v=2.1.4"></script>
	<script src="${ctx }/js/bootstrap.min.js?v=3.3.6"></script>

	<!-- Peity -->
	<script src="${ctx }/js/plugins/peity/jquery.peity.min.js"></script>

	<!-- jqGrid -->
	<script src="${ctx }/js/plugins/jqgrid/i18n/grid.locale-cn.js?0820"></script>
	<script src="${ctx }/js/plugins/jqgrid/jquery.jqGrid.min.js?0820"></script>

	<!-- jQuery Validation plugin javascript-->
	<script src="${ctx }/js/plugins/validate/jquery.validate.min.js"></script>
	<script src="${ctx }/js/plugins/validate/messages_zh.min.js"></script>

	<!-- layer javascript -->
	<script src="${ctx }/js/plugins/layer/layer.min.js"></script>

	<!-- jQuery form-->
	<script src="${ctx }/js/jquery.form.js"></script>


	<!-- 自定义js -->
	<script src="${ctx }/js/content.js?v=1.0.0"></script>
	<!-- Sweet alert -->
	<script src="${ctx }/js/plugins/sweetalert/sweetalert.min.js"></script>
	<script
		src="${ctx}/js/plugins/bootstrap-select/bootstrap-select.min.js"></script>
	<!-- bootstrap-switch js -->
	<script src="${ctx }/js/plugins/bootstrap-switch/bootstrap-switch.js"></script>
	<script>
		$(document).ready(function() {
			$.jgrid.defaults.styleUI = 'Bootstrap';
			//绑定表格数据
			$("#table_list").jqGrid({
				url : "${ctx }/monit/loadDataGrid",
				datatype : "json",
				height : "calc(100% - 190px)",
				autowidth : true,
				shrinkToFit : true,
				multiselect : true,
				multiboxonly : true,
				rowList : [ 10, 20, 30 ],
				root : "rows",
				colNames : [ '主键ID','场景名称', '管理部门', '监控状态', '管理时间' ],
				colModel : [
					
				{
						name : 'pk_scene_id',
						width : '30%',
				}, 
				{
					name : 'scene_name',
					width : '25%'
				}, 
				{
					name : 'org_name',
					width : '20%'
				}, {
					name : 'scene_status',
					width : '8%',
					formatter : function(cellvalue, options, rowObject) {
						if (cellvalue == 1) {
							return "开启";
						} else {
							return "关闭";
						}

					}

				}, {
					name : 'create_time',
					width : '15%',
					formatter : function(cellvalue, options, rowObject) {
						return formatDateString(cellvalue);
					}
				} ],
				sortable : true,
				sortname : 'create_time',
				sortorder : 'asc',
				pager : "#pager_list",
				caption : "场景配置列表",
			});
		});

		// jqGrid 自动调整宽度
		$(window).bind('resize', function() {
			var width = $('.jqGrid_wrapper').width();
			$('#table_list').setGridWidth(width);
		});

		//绑定add表单提交事件,异步提交表单
	$('#addSceneForm').submit(function() {
			var fromUrl = "${ctx }/monit/addScene";
			$.ajax({			
				type : "post",
				url : fromUrl,
				data : $('#addSceneForm').serialize(),
				success : function(data) {			
				if (data.errcode == "0")
					{
					layer.alert(data.errmsg,{icon : 1},
					function(index) {				
						//刷新表格数据		
						$("#table_list").trigger("reloadGrid");
						//清空表单数据														
						$("#addSceneForm :input").not(":button, :submit, :reset, :hidden")																
						.val("").removeAttr("checked").remove("selected");		
						//关闭add窗口										
						$('#addSceneWin').modal('hide');		
						parent.location.reload();
						//关闭提示框														
						layer.close(index);														
					});												
					} else {												
						layer.alert(data.errmsg, {icon : 2	})													
					}											
				   },
				   error : function() {			
										
						layer.alert('添加业务场景配置失败！', {
							icon : 2
						});
						}
					});
					return false;
				});

		//绑定修改按钮事件
		$('#addScene').click(function() {
			$('#addSceneWin').modal('show');
		});

		//绑定修改按钮事件
		$('#editScene').click(
				function() {
					var selectedId = $("#table_list").jqGrid("getRowData",$("#table_list").jqGrid("getGridParam", "selarrrow")).pk_scene_id;
					if (selectedId == null || selectedId == "") {
						layer.alert('请选择一项！', {
							icon : 0
						});
						return;
					}
					var frameSrc = "${ctx}/monit/editScene?pk_scene_id="+ selectedId;
					$("#editSceneFrame").attr("src", frameSrc);
					$('#editSceneWin').modal('show');
				});

		function editWinClose(flag) {
			if (flag == "0") {
				$("#table_list").trigger("reloadGrid");
			}
			parent.location.reload();
			//关闭edit窗口
			$("#editSceneFrame")[0].contentWindow.document.write('');
			$("#editSceneFrame")[0].contentWindow.close();
			$('#editSceneWin').modal('hide');
		}

		//绑定删除事件
		$('#delScene').click(
				function() {
					var selectedIds = $("#table_list").jqGrid("getGridParam",
					"selarrrow");
					var ids = "";
					for (var i = 0; i < selectedIds.length; i++) {
						 var v_pk_scene_id=($("#table_list").jqGrid("getRowData",selectedIds[i])).pk_scene_id;
						  if (ids != '') ids += ',';
							ids += v_pk_scene_id;
					}
					if (selectedIds == "") {
						layer.alert('请选择一项！', {
							icon : 0
						});
						return;
					}
					swal({
						title : "您确定要删除这条信息吗",
						text : "删除后将无法恢复，请谨慎操作！",
						type : "warning",
						showCancelButton : true,
						confirmButtonColor : "#DD6B55",
						confirmButtonText : "删除",
						closeOnConfirm : false
					}, function() {
						$.ajax({
							type : "post",
							dataType : "json",
							url : "${ctx }/monit/deleteScene",
							data : {'ids':ids},
							success : function(data) {
								if (data.errcode == "0") {
									//刷新表格数据
									$("#table_list").trigger("reloadGrid");
									swal("删除成功！", "您已经永久删除了这条信息。", "success");
									parent.location.reload();
								} else {
									swal("删除用户失败！", "您未删除这条信息。", "error");
								}
							},
							error : function() {
								swal("删除用户失败！", "您未删除这条信息。", "error");
							}
						});
					});
				});
		function formatDateString(date) {
			if (date == '' || date == undefined) {
				return '';
			} else {
				var d = new Date();
				d.setTime(date);
				var year = d.getFullYear();
				var month = d.getMonth() + 1;
				if (month * 1 < 10)
					month = '0' + month;
				var date = d.getDate();
				if (date * 1 < 10)
					date = '0' + date;
				var hours = d.getHours();
				if (hours * 1 < 10)
					hours = '0' + hours;
				var m = d.getMinutes();
				if (m * 1 < 10)
					m = '0' + m;
				var s = d.getSeconds();
				if (s * 1 < 10)
					s = '0' + s;
				return year + "-" + month + "-" + date + " " + hours + ":" + m
						+ ":" + s;
			}
		}

		//监控开关
		$('#share_switch_div input').bootstrapSwitch({
			onText : "ON",
			offText : "OFF",
			onColor : "primary",
			offColor : "danger",
			size : "small",
			state : false,
			onInit : function(event, state) {//初始化开关
				$("#scene_status").val("0");
			},
			onSwitchChange : function(event, state) {//开关状态发生改变时
				if (state == true) {
					$("#scene_status").val("1");
				} else {
					$("#scene_status").val("0");
				}
			}
		});
	</script>
</body>
</html>